#include "dncategoryselectdlg.h"
#include "ui_dncategoryselectdlg.h"
//#include "global.h"
#include "tdomain.h"

DnCategorySelectDlg::DnCategorySelectDlg(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DnCategorySelectDlg)
{
    ui->setupUi(this);

    m_tDomain = static_cast<TDomain *>(QWidget::parentWidget());

    m_model = new QStandardItemModel();
    m_model->setHorizontalHeaderLabels({"域名类别管理"});
    ui->treeView->setModel(m_model);

    //QSqlDatabase m_db = g_mysqlPool.getConnection();
    m_tDomain->m_dbMySql->loadCategoryTree(m_model);
    //g_mysqlPool.releaseConnection(m_db);
    connect(ui->treeView, &QTreeView::doubleClicked, [&](const QModelIndex &index){
        slotChangeDnCategory(index);
    });
}

DnCategorySelectDlg::~DnCategorySelectDlg()
{
    delete ui;
}

void DnCategorySelectDlg::slotChangeDnCategory(QModelIndex index)
{
    QStandardItem *item = m_model->itemFromIndex(index);
    m_CategoryId = item->data(Qt::UserRole).toInt();
    int id =item->data(Qt::UserRole).toInt();
    qDebug() << "Double clicked item: " << item->text()<<id;
//    QVariantList domainIds;
//    if(!m_tDomain->m_dbMySql->updateDnCategory(id,domainIds))
//    {
//        QMessageBox::information(this,"提示","变更域名类别失败");
//        return;
//    }

}



void DnCategorySelectDlg::on_btn_ok_clicked()
{
    QModelIndex selectedIndex = ui->treeView->selectionModel()->currentIndex();
    if (!selectedIndex.isValid()) {
        QMessageBox::warning(this, "警告", "请选择一个类别");
        return;
    }
    QStandardItem *item = m_model->itemFromIndex(selectedIndex);
    QString msg = item->text();
    if(msg=="全部记录")
    {
        QMessageBox::warning(this, "警告", "该类别不可选");
        return;
    }
    if(item->hasChildren())
    {
        msg = msg+  " 非子类别,请选择一个子类别";
        QMessageBox::warning(this, "警告", msg);
        return;
    }
    m_CategoryId = item->data(Qt::UserRole).toInt();
    close();
}


void DnCategorySelectDlg::on_btn_cancel_clicked()
{
    this->close();
}

